<?php

class SiteController extends Controller {

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            //captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xA9BED8,
                'minLength' => 5,
                'maxLength' => 5,
                'fontFile' => _APP_PATH_ . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . 'ttf' . DIRECTORY_SEPARATOR . 'arial.ttf',
            ),
        );
    }

    public function actionIndex() {
        $this->layout = 'home';
        $this->render('index', array(
        ));
    }

    public function actionSwitchLanguage() {
        $app = Yii::app();
        if (isset($_GET['_lang']) && array_key_exists($_GET['_lang'], $app->params['langArr'])) {
            $app->language = $_GET['_lang'];
            $app->session['_lang'] = $_GET['_lang'];
        }

        $this->redirect($app->request->urlReferrer);
    }

    public function actionSearch() {
        $keyword = ($_GET['q']) ? $_GET['q'] : "";
        if ($keyword == "")
            $this->redirect(Yii::app()->createUrl("site/index"), true, 301);
        else {
//            $keyword = addslashes($keyword);
//            $filters = array();
//            $page = (isset($_GET['page']) && $_GET['page']) ? $_GET['page'] : 1;
//            $filters['page'] = $page;
//            $filters['sort'] = "sorder";
//            $filters['keyword'] = $keyword;
//            $this->search['keyword'] = $keyword;
//
//            $article = new Article();
//            $article->status = Article::STATUS_AVAILABLE;
//            $article->is_hot = '';
//            $article->updated_by = '';
//            $article->is_feature = '';
//            $article->hits = '';
//            $dataProvider = $article->search($filters);
//
//            $this->layout = "home";
//            $this->render('searchResult', array(
//                'dataProvider' => $dataProvider,
//            ));
        }
        Yii::app()->end();
    }

    public function actionError() {
        $error = Yii::app()->errorHandler->error;
        if ($error) {
            if (Yii::app()->request->isAjaxRequest) {
                echo $error['message'];
            } elseif ($error['code'] == 404) {
                // request not found, redirect to search
                $query = Common::uriToString(urldecode($_SERVER['REQUEST_URI']));
//                header("Content-Type: text/plain; charset=utf-8");
//                echo $query;
                $this->redirect($this->createUrl("site/search", array("q" => $query)));
            } elseif (in_array($error['code'], array(400, 403, 500, 503))) {
                $this->render("error/error{$error['code']}", $error);
            } else {
                $this->render("error/error", $error);
            }
        }
        Yii::app()->end();
    }

    public function actionLoadLanguageJs() {
        echo json_encode(include _APP_PATH_ . '/protected/messages/' . Yii::app()->language . "/JS.php");
        Yii::app()->end();
    }

    public function actionContact() {
        $model = new Contact();
        if (isset($_POST['Contact'])) {
            $model->attributes = $_POST['Contact'];
            //Clean data
            $p = new CHtmlPurifier();
            $model->content = $p->purify($model->content);
            foreach ($model->attributes as $key => $val) {
                $model->$key = $val;
            }
            if ($model->validate()) {
                $model->contact_date = date('Y-m-d H:i:s');
                $model->ip = CHttpRequest::getUserHostAddress();
                if ($model->insert()) {
//                    // send to admin
//                    $paramsAdmin = array(
//                        'admin' => Yii::app()->params['gmailInfo']['name'],
//                        'to_email' => Yii::app()->params['gmailInfo']['username'],
//                        'full_name' => $model->full_name,
//                        'email' => $model->email,
//                        'phone' => $model->phone,
//                        'title' => $model->title,
//                        'content' => $model->content,
//                        'date' => $model->contact_date,
//                    );
//                    Common::sendEmail('email_contact_to_admin', $paramsAdmin);
//                    //1-Send email to member
//                    $params = array(
//                        'name' => $model->full_name,
//                        'to_email' => $model->email
//                    );
//                    Common::sendEmail('contact', $params);
                    Yii::app()->user->setFlash('contact', Yii::t('FrontEnd', 'Send contact complates'));
                }
            }
        }
        $this->layout = 'main';
        $this->render('contact', array(
            'model' => $model
        ));
        Yii::app()->end();
    }

    public function actionAbout() {
        $cacheName = '_about_';
        if (!$dataCached = Yii::app()->cache->get($cacheName)) {
            $model = About::model()->findByPk(1);
            //Save to cache
            $systemCacheTimeLife = (isset(Yii::app()->params->systemCacheTimeLife)) ? Yii::app()->params->systemCacheTimeLife : 3600; // =>(secons)
            Yii::app()->cache->set($cacheName, $model, $systemCacheTimeLife);
        } else {
            $model = $dataCached;
        }
        $this->layout = 'main';
        $this->render('about', array(
            'model' => $model
        ));
        Yii::app()->end();
    }

}